//
//  NYSubjectBookPageInfoCell.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/5.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYSubjectBookPageInfoCell.h"
@interface NYSubjectBookPageInfoCell ()
@property(nonatomic, strong)  TTTAttributedLabel *desLabel;
@property(nonatomic, strong) NSMutableArray <TTTAttributedLabel *>*labelViewsArray;
@end
@implementation NYSubjectBookPageInfoCell
+ (CGFloat)heightForObject:(id)object identifier:(NSString *)identifier atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
        return __WGHeight(251);
}
- (BOOL)shouldUpdateCellWithObject:(NYSubjectPageInfoModel *)object atIndexPath:(NSIndexPath *)indexPath
{
        self.indexPath = indexPath;
        self.pageInfoModel = object;
        return YES;
}

- (void)setPageInfoModel:(NYSubjectPageInfoModel *)subjectCountModel
{
        _pageInfoModel = subjectCountModel;
        [self updateContent];
}
- (void)updateContent
{
        [super updateContent];
        self.desLabel.attributedText = [self createDesAttrbuteString];
        
        NSArray <NSString *>*values = @[@"",@"错题数量",@"订正数量",
                                        @"全部",NOT_NULL_STRING(self.pageInfoModel.all_num, @"0"),NOT_NULL_STRING(self.pageInfoModel.all_correction_num, @"0"),
                                        @"本周",NOT_NULL_STRING(self.pageInfoModel.week_num, @"0"),NOT_NULL_STRING(self.pageInfoModel.week_correction_num, @"0"),
                                        @"今日",NOT_NULL_STRING(self.pageInfoModel.today_remaining_num, @"0"),NOT_NULL_STRING(self.pageInfoModel.today_correction_num, @"0")];
        
        
        [values enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [self.labelViewsArray objectAtIndex:idx].text = obj;
        }];
}
- (NSAttributedString *)createDesAttrbuteString
{
        NSDictionary *norAttr = @{
                                  NSFontAttributeName : WGSystem14Font,
                                  NSForegroundColorAttributeName : UIColorHex(0x333333)
                                  };
        NSDictionary *orangeAttr = @{
                                  NSFontAttributeName : WGSystem14Font,
                                  NSForegroundColorAttributeName : UIColorHex(0xFA8D00)
                                  };
        NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc] initWithString:@"我的错题情况，今日错题情况：错题数量" attributes:norAttr];
        [attrM appendAttributedString:[[NSAttributedString alloc] initWithString:NOT_NULL_STRING_DEFAULT_EMPTY(self.pageInfoModel.info_today_num) attributes:orangeAttr]];
        [attrM appendAttributedString:[[NSAttributedString alloc] initWithString:@"，其中已改正" attributes:norAttr]];
        [attrM appendAttributedString:[[NSAttributedString alloc] initWithString:NOT_NULL_STRING_DEFAULT_EMPTY(self.pageInfoModel.info_today_correction_num) attributes:orangeAttr]];
        [attrM appendAttributedString:[[NSAttributedString alloc] initWithString:@"，剩余错题" attributes:norAttr]];
        [attrM appendAttributedString:[[NSAttributedString alloc]
                                       initWithString:NOT_NULL_STRING_DEFAULT_EMPTY(self.pageInfoModel.info_today_surplus_num) attributes:orangeAttr]];
        return attrM;
}
- (void)setupView
{
        [super setupView];
        
        UIView *contentView = [[UIView alloc] init];
        [self.contentView addSubview:contentView];
        [contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.contentView).mas_offset(__WGHeight(15));
                make.left.equalTo(self.contentView).mas_offset(__WGHeight(16));
                make.bottom.equalTo(self.contentView).mas_offset(-__WGHeight(28)).priorityLow();
                make.right.equalTo(self.contentView).mas_offset(-__WGHeight(16));
        }];
        
        self.desLabel = [self createNormalLabel];
        self.desLabel.textAlignment = NSTextAlignmentLeft;
        self.desLabel.numberOfLines = 0;
        self.desLabel.qmui_borderPosition = QMUIBorderViewPositionNone;
        [contentView addSubview:self.desLabel];
        self.labelViewsArray = [NSMutableArray<TTTAttributedLabel *> arrayWithCapacity:12];
        
        QMUIGridView *gridView = [[QMUIGridView alloc] initWithColumn:3 rowHeight:__WGHeight(40)];
        gridView.qmui_borderPosition = QMUIBorderViewPositionLeft | QMUIBorderViewPositionRight | QMUIBorderViewPositionTop;
        
        NSArray <NSString *>*titles = @[@"",@"错题数量",@"订正数量"];
        [titles enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                TTTAttributedLabel *tmpLabel = [self createTitleLabel];
                tmpLabel.text = obj;
                [gridView addSubview:tmpLabel];
                [self.labelViewsArray addObject:tmpLabel];
        }];
        NSArray <NSString *>*contents = @[@"全部",@"0",@"0",
                                          @"本周",@"0",@"0",
                                          @"今日",@"0",@"0"];
        
        [contents enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                TTTAttributedLabel *tmpLabel = [self createNormalLabel];
                tmpLabel.text = obj;
                [gridView addSubview:tmpLabel];
                [self.labelViewsArray addObject:tmpLabel];
        }];
        
        [contentView addSubview:gridView];
        
        [self.desLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.left.right.equalTo(contentView);
        }];
        [gridView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.desLabel.mas_bottom).offset(__WGHeight(16));
                make.left.right.bottom.equalTo(contentView);
                make.height.mas_equalTo(__WGHeight(161));
        }];
}
- (TTTAttributedLabel *)createNormalLabel
{
        TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        label.font = WGSystem14Font;
        label.textColor = UIColorHex(0x333333);
        label.textAlignment = NSTextAlignmentCenter;
        label.qmui_borderPosition = QMUIBorderViewPositionBottom;
        return label;
}
- (TTTAttributedLabel *)createTitleLabel
{
        TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        label.font = WGSystem14Font;
        label.backgroundColor = UIColorHex(0xE5F1FF);
        label.textColor = UIColorHex(0x3696FF);
        label.textAlignment = NSTextAlignmentCenter;
        label.qmui_borderPosition = QMUIBorderViewPositionBottom;
        return label;
}
@end
